using System.IO;
using AvaloniaApplication1.IServices;
using AvaloniaApplication1.Models;
using AvaloniaApplication1.Models.DTOModel;
using AvaloniaApplication1.Service;
using AvaloniaApplication1.Services;
using AvaloniaApplication1.ViewModels;
using AvaloniaApplication1.ViewModels.CardViewModel;
using AvaloniaApplication1.Views;
using AvaloniaApplication1.Views.CardView;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SukiUI.Dialogs;
using SukiUI.Toasts;

namespace AvaloniaApplication1.Util;

/// <summary>
/// Service注册扩展类
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 原始的Service注册方式(未使用)
    /// </summary>
    /// <param name="services"></param>
    public static void AddServicesNaive(this IServiceCollection services)
    {
        // 创建(app settings)配置对象
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("AppSettings.json", optional: false, reloadOnChange: true)
            .Build();
        services.Configure<AppSettings>(configuration.GetSection("AppSettings"));
        // 注册 ViewModels
        services.AddSingleton<MainWindowViewModel>();
        services.AddSingleton<HomePageViewModel>();
        services.AddSingleton<SettingsPageViewModel>();
        services.AddSingleton<DevicePageViewModel>();
        services.AddSingleton<TmphumViewModel>();
        services.AddSingleton<DataGridViewModel>();
        services.AddSingleton<LineChartViewModel>();
        services.AddSingleton<MqttDeviceCardViewModel>();
        AppLogger.Information("ViewModels 注册完成");
        // 注册 Views（如果需要通过 DI 构造 View）
        services.AddTransient<MainWindow>();
        services.AddTransient<HomePageView>();
        services.AddTransient<SettingsPageView>();
        services.AddTransient<DevicePageView>();
        services.AddTransient<TmphumView>();
        services.AddTransient<DataGridView>();
        services.AddTransient<LineChartView>();
        AppLogger.Information("Views 注册完成");
        //注册必要Service
        services.AddSingleton<ViewLocator>(); // 注册 ViewLocator用于根据ViewModel查找对应的View
        services.AddTransient<MqttCardModel>();
        // 注册 Services（可选）
        services.AddSingleton<ThemeService>();
        services.AddSingleton<IMqttDeviceManager, MqttDeviceManager>();
        // services.AddSingleton<MqttTopicConfig>();
        //注册DialogService
        services.AddSingleton<ISukiDialogManager, SukiDialogManager>();
        services.AddSingleton<ISukiToastManager, SukiToastManager>();
        services.AddTransient<AddDeviceDialogView>();
        services.AddSingleton<AddDeviceDialogViewModel>();
        services.AddTransient<DeviceSettingView>();
        services.AddSingleton<DeviceSettingViewModel>();
    }

    /// <summary>
    /// 使用Scrutor进行Service注册
    /// </summary>
    /// <param name="container"></param>
    public static void AddServiceWithScrutor(this IServiceCollection container)
    {
        

        
        container.AddSingleton<TopicDisplayViewModel>();
        // 注册 ViewModels
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Namespace.Contains("ViewModels")&&type.Name != "TopicDisplayViewModel"))
            .AsSelf()
            .WithSingletonLifetime()
        );
        AppLogger.Information("ViewModels 注册完成");
        // 注册 Views
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Namespace.Contains("Views")))
            .AsSelf()
            .WithTransientLifetime()
        );
        AppLogger.Information("Views 注册完成");
        // 注册 Manager
        container.Scan(scan => scan
            .FromAssemblyOf<Program>()
            .AddClasses(cls => cls.Where(type => type.Name.EndsWith("Manager")))
            .AsImplementedInterfaces()
            .WithSingletonLifetime()
        );
       


        //注册特殊的Service
  
        container.AddSingleton<ISukiDialogManager, SukiDialogManager>();
        container.AddSingleton<ISukiToastManager, SukiToastManager>();
        container.AddSingleton<ModbusConfig>();
        
        container.AddSingleton<ViewLocator>(); // 注册 ViewLocator用于根据ViewModel查找对应的View
        container.AddTransient<MqttCardModel>();
        container.AddSingleton<ThemeService>();
    }
}